package com.elepant;

import com.elepant.tank.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * Hello world!
 */
public class App {
      public static void main(String[] args) throws InterruptedException {

         //transHexToBinary();

         //transStringToBinary();

        //transStringToHex();

        //testFloors();
         TankFrame tankFrame = new TankFrame();

         //初始化敌军坦克
        for (int i = 0; i < 15; i++) {
            tankFrame.tanks.add(new Tank(50 + i * 80, 150, Direction.Down, Group.Bad, tankFrame));
        }

       /* while (true){
            Thread.sleep(100);
            tankFrame.repaint();
            //System.out.println("----------------");
        }
*/
        new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(25);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                tankFrame.repaint();
            }
        }).start();

        new Thread(()->{
            Audio a = new Audio("audio/war1.wav");
            a.play();
        }).start();

    }

    private static void transHexToBinary() {
        System.out.println("输入十六进制：");
        Scanner scanner = new Scanner(System.in);
        while (true) {
            String hexStr = scanner.next();
            int num = Integer.parseInt(hexStr, 16);
            String result = Integer.toBinaryString(num);
            String formatResult = String.format("%8s", result).replaceAll(" ", "0");
            String restul2 = prettyBinary(formatResult, 8, " ");
            System.out.println(hexStr + " -> " + restul2);
        }
    }

    /**
     * String -> char -> int -> binary  美化：auto-cast zero pads
     */
    private static void transStringToBinary() {
        System.out.println("输入字符串：");
        Scanner scanner = new Scanner(System.in);
        while (true) {
            StringBuilder result = new StringBuilder();
            String str = scanner.next();
            char[] chars = str.toCharArray();
            for (char aChar : chars) {
                String temp = String.format("%8s", Integer.toBinaryString(aChar)).replaceAll(" ", "0");
                System.out.println(aChar + " -> " + temp);
                result.append(temp);
            }
            String pretty = prettyBinary(result.toString(), 8, " ");
            System.out.println(pretty);

        }
    }

    private static void transStringToHex() {
        System.out.println("输入字符串：");
        Scanner scanner = new Scanner(System.in);
        while (true) {
            StringBuilder result = new StringBuilder();
            String str = scanner.next();
            byte[] bytes = str.getBytes();
            String hex = trasBytesToHex(bytes);
            System.out.println(hex);
        }
    }


    public static String trasBytesToHex(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if (hex.length() < 2) {
                sb.append(0);
            }
            sb.append(hex);
        }
        return sb.toString();
    }

    public static String prettyBinary(String binary, int blockSize, String separator) {
        List<String> result = new ArrayList<>();
        int index = 0;
        while (index < binary.length()) {
            result.add(binary.substring(index, Math.min(index + blockSize, binary.length())));
            index += blockSize;
        }
        return result.stream().collect(Collectors.joining(separator));
    }

    //将楼层按位显示
    public static void testFloors() {
        int tempResult = 0x000000000000;
        int floorIndex1 = 5;
        tempResult = mergeToHex(tempResult, floorIndex1);

        String hex0 = String.format("0x%012x", Integer.valueOf(tempResult));
        String hex1 = String.format("%6s", Integer.toBinaryString(1));

        System.out.println("hex0=" + hex0);
        System.out.println("hex1=" + hex1);


        int floorIndex2 = 6;
        tempResult = mergeToHex(tempResult, floorIndex2);
    }


    /**
     * 面板合并(通过下标合并)
     *
     * @param _init       0x00000000
     * @param boardNumber
     * @return
     */
    private static int mergeToHex(int _init, int boardNumber) {
        int board1 = boardNumber - 1;//第一块面板-1 对应的编码
        // 1 << 位数   其中位数=board编码数
        int b1_hex = 1 << board1;
        int result = _init | b1_hex;

        //合并后的最终结果
        System.out.println(String.format("0x%012x", result));
        //System.out.println(result);
        return result;
    }

}
